package com.qingzhuge.sys.service.impl;

import com.google.common.collect.Maps;
import com.qingzhuge.service.BasePkLongServiceImpl;
import com.qingzhuge.sys.dao.SysDictDao;
import com.qingzhuge.sys.entity.SysDict;
import com.qingzhuge.sys.service.ISysDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author : zero.xiao
 * @description :
 * @date :2018/7/11 0011 下午 12:13
 * @since : 1.0.0
 * @modified :
 */
@CacheConfig(cacheNames = SysDictServiceImpl.CACHE_NAME)
@Service
public class SysDictServiceImpl extends BasePkLongServiceImpl<SysDictDao, SysDict> implements ISysDictService {
    static final String CACHE_NAME = "sysDictService";

    @Override
    public Map<String, Map<String, String>> getAllDict() {
        List<SysDict> list = super.findAll();
        Map<String, Map<String, String>> resultMap = Maps.newHashMap();
        for (SysDict sysDict : list) {
            if (sysDict != null) {
                String key = sysDict.getType();
                if (resultMap.get(key) == null) {
                    Map<String, String> dicMap = Maps.newHashMap();
                    resultMap.put(key, dicMap);
                }
                if (StringUtils.isNotBlank(sysDict.getParentType())) {
                    resultMap.get(key).put(sysDict.getCode(), sysDict.getName());
                }
            }
        }
        return resultMap;
    }

    @Override
    public Map<String, String> queryDictByType(String key) {
        List<SysDict> list = this.queryByType(key);
        Map<String, String> resultMap = Maps.newHashMap();
        for (SysDict sysDict : list) {
            resultMap.put(sysDict.getCode(), sysDict.getName());
        }
        return resultMap;
    }

    @Override
    public List<SysDict> queryByType(String type) {
        return baseDao.queryByType(type);
    }

    @Override
    public List<SysDict> queryByTypeAndCode(String type, String code) {
        return baseDao.queryByTypeAndCode(type,code);
    }
}

